package cn.enjoyedu.sendtype;

import cn.enjoyedu.config.BusiConst;
import cn.enjoyedu.config.KafkaConst;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.Future;

/**
 * @author King老师   享学课堂 https://enjoy.ke.qq.com
 * 类说明：同步发送消息--未来某个时候get发送结果
 */
public class KafkaFutureProducer {
    

    public static void main(String[] args) {
        
        Properties properties = KafkaConst.producerConfig(StringSerializer.class, StringSerializer.class);
        
        try (KafkaProducer<String,String> producer= new KafkaProducer<>(properties)){
            
            /*待发送的消息实例*/
            ProducerRecord<String,String> record;
            try {
                record = new ProducerRecord<>(
                        BusiConst.HELLO_TOPIC, "teacher10", "james");
                
                //MARK 要服务器回复
                Future<RecordMetadata> future = producer.send(record);
                
                System.out.println("do other sth");
                //MARK  阻塞到服务器回复 
                RecordMetadata recordMetadata = future.get();
                if(null!=recordMetadata){
                    //MARK 如果成功发送 服务器会还我一个回复,里面除了我已经知道信息, 还会告我生产偏移量
                    System.out.println("offset:"+recordMetadata.offset()+"-" +"partition:"+recordMetadata.partition());
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        } 
    }




}
